package cn.cdeden.knowledge.controller.online;


import cn.cdeden.common.core.domain.R;
import cn.cdeden.common.core.utils.page.TableDataInfo;
import cn.cdeden.common.satoken.utils.LoginHelper;
import cn.cdeden.knowledge.controller.online.search.SearchFilesInfoFactory;
import cn.cdeden.knowledge.domain.dto.FilesLabelExistence;
import cn.cdeden.knowledge.domain.dto.FilesTree;
import cn.cdeden.knowledge.domain.dto.SearchFilesInfoDto;
import cn.cdeden.knowledge.service.FilesLabelService;
import cn.cdeden.system.api.model.LoginUser;
import lombok.RequiredArgsConstructor;
import lombok.extern.slf4j.Slf4j;
import org.apache.commons.collections4.CollectionUtils;
import org.springframework.data.domain.Pageable;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.*;

import java.util.List;

/**
 * 在线文件:搜查文件
 */
@Controller
@Slf4j
//@Api(tags = "在线文件:搜查文件")
@RestController
@RequiredArgsConstructor
@RequestMapping("/api/search/files")
public class SearchFilesInfoController {

    private final FilesLabelService labelService ;

    private final SearchFilesInfoFactory factory;

    /**
     * 在线文件:搜索文件信息
     * @param type
     * @param infoDto
     * @param pageable
     * @return
     */
//    @Log("在线文件:搜索文件信息")
//    @ApiOperation("在线文件:搜索文件列表")
    @ResponseBody
//    @PostMapping(value = "/info")
    @GetMapping(value = "/info")
    public R<TableDataInfo<FilesTree>> searchFilesInfo(String type , SearchFilesInfoDto infoDto, Pageable pageable) {
//        Long id = FileUserUtils.getUser().getId();//用户编号
        Long id = LoginHelper.getUserId();
        infoDto.setUserId(id.toString());  //设置观看用户
        List<String> labelIds = infoDto.getLabelIds();
        if (CollectionUtils.isNotEmpty(labelIds)){
            String labelType = "user";
            boolean b = labelService.queryLabelTypeByIdExistence(
                FilesLabelExistence
                    .builder()
                    .labelIds(labelIds)
                    .labelType(labelType)
                    .userId(infoDto.getUserId())
                    .build()
            );
            if (!b){
                throw new RuntimeException("查询错误的标签编号");
            }
        }

        List<String> labelTypeIds = infoDto.getLabelTypeIds();;
        if (CollectionUtils.isNotEmpty(labelTypeIds)){
            String labelType = "user";
            switch (type){
                case "team":
                    labelType = "team";
                    break;
                case "enterprise":
                    labelType = "sys";
                    break;
                case "personal":
                default:
                    throw new RuntimeException("查询错误的级别标签编号");
            }
            boolean b = labelService.queryLabelTypeByIdExistence(
                FilesLabelExistence
                    .builder()
                    .labelIds(labelTypeIds)
                    .labelType(labelType)
                    .teamId(infoDto.getTeamId())
                    .userId(infoDto.getUserId())
                    .build()
            );
            if (!b){
                throw new RuntimeException("查询错误的没有的级别标签编号");
            }
        }
        TableDataInfo<FilesTree> filesTreePageInfo = factory.searchInfo(type, infoDto, pageable);
        return R.ok(filesTreePageInfo);
    }
}
